package create;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocal;
import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocalHome;
import org.multimedia.jolitorask.security.AuthServlet;
import org.multimedia.jolitorask.telephone.TelephoneLocal;
import org.multimedia.jolitorask.telephone.TelephoneLocalHome;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;

/**
 * Servlet implementation class for Servlet: AddPhone
 *
 */
 public class AddPhone extends AuthServlet {
		private static final long serialVersionUID = 1L;
		private static final String JNDI_NAME_WRAPPER = "asteriskWrapperSessionLocal";
		private static final String JNDI_NAME = "telephoneLocal";
		
		public AddPhone() {
			super();
		}   	
		
		@SuppressWarnings("unchecked")
		protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

			
			    res.setContentType("text/plain");
			    PrintWriter out = res.getWriter();
			    
			    String utilisateur;
			    String nom;
			    String password;
			    String account;
			    String email;
			    
			    /* On recupere le nom */
			    if( (utilisateur=req.getParameter("login")) == null){
			    	out.println(0);
			    	return;
			    }
			    
			    /* On recupere le nom */
			    if( (nom=req.getParameter("name")) == null){
			    	out.println(0);
			    	return;
			    }
			   
			    /* On recupere le password */
			    if( (password=req.getParameter("password")) == null){
			    	out.println(0);
			    	return;
			    }
			    
			    /* On recupere le client */
			    if( (account=req.getParameter("account")) == null){
			    	out.println(0);
			    	return;
			    }
			   
			    email = req.getParameter("email");
			    
			    /* Creation */
			    Context initContext = null;
			    Hashtable env = new Hashtable(1);
			    Object beanObject = null;
			    
			    if( !authentificate(req) ){
			    	out.println(false);
			    	return;
			    }
			    	
			    try {
			    	
					Collection<UtilisateurLocal> liste = client.getUtilisateurs();
					
					Iterator<UtilisateurLocal> it = liste.iterator();
					
					UtilisateurLocal user = null;
					while( it.hasNext() ){
						UtilisateurLocal u = it.next();
						
						if(u.getLogin().equals(utilisateur)){
							user = u;
							break;
						}
							
					}
					
					if( user == null){
						out.println(0);
						return;
					}
					
					/*** changer pour user ***/
					initContext = new javax.naming.InitialContext(env);
			    	beanObject = initContext.lookup(JNDI_NAME);
			    	TelephoneLocalHome home2 = (TelephoneLocalHome) beanObject;
					
			    	Iterator<TelephoneLocal> it2 = user.getTelephones().iterator();
			    	
			    	while(it2.hasNext()){
			    		TelephoneLocal tel = it2.next();
			    		
			    		if(tel.getNom().equals(nom)){
			    			out.println(0);
			    			return;
			    		}
			    	}
			    	
					TelephoneLocal localPhone = home2.create(null, nom, password, user);
					
					//Objet Asterisk
					beanObject = initContext.lookup(JNDI_NAME_WRAPPER);
					AsteriskWrapperLocalHome homeA = (AsteriskWrapperLocalHome)beanObject;
					AsteriskWrapperLocal wrapper = homeA.create();
					wrapper.init(account, utilisateur);
					int numero = wrapper.addNewPhone(((Integer)user.getPrimaryKey()).intValue(), ((Integer)localPhone.getPrimaryKey()).intValue(), user.getNom(), user.getPrenom(), password, email);
					
					localPhone.setNumero(numero+"");
					out.println(numero);
					
				}
			    catch(CreateException e) {
			    	
			    	out.println(0);
			    	return;
			    }
			    catch(NamingException e2){
			    	out.println(0);
			    	return;
			    }catch(SQLException e3){
			    	out.println(0);
			    	return;
			    }
			    
		}  	
		
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		}   	   	  	    
}